package com.lagou.controller;


import com.github.pagehelper.PageInfo;
import com.lagou.domain.ResponseResult;
import com.lagou.domain.Role;
import com.lagou.domain.User;
import com.lagou.domain.UserVO;
import com.lagou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findAllUserByPage")
    public ResponseResult findAllUserByPage(@RequestBody UserVO userVO){
        PageInfo pageInfo = userService.findAllUserByPage(userVO);
        return new ResponseResult(true,200,"响应成功",pageInfo);
    }

    @RequestMapping("/updateUserStatus")
    public ResponseResult updateUserStatus(Integer id,String status){
        if ("ENABLE".equalsIgnoreCase(status)){
            status = "DISABLE";
        }else {
            status = "ENABLE";
        }
        userService.updateUserStatus(id,status);
        return new ResponseResult(true,200,"修改状态成功",status);
    }

    //用户登录

    @RequestMapping("/login")
    public ResponseResult login( User user, HttpServletRequest request) throws Exception {
        User login = userService.login(user);
        if(login!=null){
            Map<String,Object> map = new HashMap<>();
            String access_token = UUID.randomUUID().toString();
            map.put("access_token",access_token);
            map.put("user_id",login.getId());

            HttpSession session = request.getSession();
            session.setAttribute("user_id",login.getId());
            session.setAttribute("access_token",access_token);
            return new ResponseResult(true,200,"登录成功",map);
        }else {
            return new ResponseResult(true,200,"登录失败",null);
        }
    }

    //获取用户拥有的角色
    @RequestMapping("/findUserRoleById")
    public ResponseResult findUserRoleById(int id){
        List<Role> role = userService.findUserRelationRoleById(id);
        return new ResponseResult(true,200,"分配角色回显成功",role);
    }

    @RequestMapping("/userContextRole")
    public ResponseResult userContextRole(@RequestBody UserVO userVo){
         userService.userContextRole(userVo);
         return new ResponseResult(true,200,"分配角色成功",null);
    }

    //获取用户权限
    @RequestMapping("/getUserPermissions")
    public ResponseResult getUserPermissions(HttpServletRequest request){
        //获取请求头中的    token
        String header = request.getHeader("Authorization");

        //获取session中的access_token
        HttpSession session = request.getSession();
        String access_token = (String) session.getAttribute("access_token");

        //判断
        if(header.equals(access_token)){
            int userId = (int) session.getAttribute("user_id");
            return userService.getUserPermissions(userId);
        }else {
           return new ResponseResult(false,400,"获取失败","");
        }
    }
}
